/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.TimeClockServer.messages.rx;

/**
 * This class is a key component to be used when de-constructing messages
 * received from the clock.  It breaks down the received messages into its
 * component parts based on the presence of iterators.
 *
 * Various constructors allow the message to be specified in text form (with
 * mnemonic) or binary (with non-printing control codes).
 *
 * The splitter can be used in simple index form where the consumer requests
 * token number 'n' or in keyed form where the consumer specifies a field name.
 * If using the second form then the user must supply a second class that maps
 * keys to index numbers.
 */
import TimeRecording.TimeClockServer.MessageFormatter;

/**
 *
 * @author david
 */
public class SplitMessage {

    // The various parts of the message
    private String[] tokens;

    // An optional set of keys for keyed access to the parts
    private SplitMessage keys;

    /**
     * This constructor simply splits the binary message into its component
     * parts.  The message is supplied in binary form.
     *
     * @param bytes the clock message in binary (non-mnemonic) form
     */
    public SplitMessage(byte[] bytes) {
        tokens = MessageFormatter.splitMessage(bytes);
    }

    /**
     * This constructor simply splits the text message into its component parts.
     * The message is supplied in text form containing mnemonics rather than
     * control codes.
     *
     * @param text the clock message in textual (human-friendly) form
     */
    public SplitMessage(String text) {
        this(MessageFormatter.textToBytes(text));
    }

    /**
     * This constructor simply splits the binary message into its component
     * parts.  The message is supplied in binary form.  A set of keys is
     * supplied so that the keyed getPart method can be used.
     *
     * @param bytes the clock message in binary (non-mnemonic) form
     */
    public SplitMessage(String text, SplitMessage keys) {
        this(text);
        this.keys = keys;
    }

    /**
     * This constructor simply splits the text message into its component parts.
     * The message is supplied in text form containing mnemonics rather than
     * control codes.  A set of keys is supplied so that the keyed getPart method
     * can be used.
     * 
     * @param text the clock message in textual (human-friendly) form
     */
    public SplitMessage(byte[] bytes, SplitMessage keys) {
        this(bytes);
        this.keys = keys;
    }

    /**
     * Simply returns the number of parts to the message.
     *
     * @return the number of message parts (including all separators)
     */
    public int partCount() {
        return tokens.length;
    }

    /**
     * This method simply returns the 'n'th part of the message.  No array
     * checking is performed; a garbage request will result in a run-time
     * exception.
     *
     * @param index the index of the part required
     * @return the 'n' part of the message in string form
     */
    public String getPart(int index) {
        return tokens[index];
    }

    /**
     * This method returns the part of the message associated with the passed
     * key value.  The method searches the keys supplied to the constructor and
     * gets the index of the matching cell.  It then returns the element in
     * the message at that same index.
     *
     * @param name the name of the key of the message part
     * @return the part of the message associated with the key
     */
    public String getPart(String name) {

        int index = 0;

        for (String key : keys.tokens) {
            if (key.equals(name)) {
                return getPart(index);
            } else {
                index++;
            }
        }
        
        return null;
    }
}